package com.ship.core.processor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ship.core.ConstantsPwdKey;
import com.ship.entity.dto.DecryptRequest;
import com.ship.entity.dto.EncryptResponse;
import com.ship.entity.dto.HttpEncryptRequest;
import com.ship.util.RSAUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 *加密解密
 *
 */
public class RequestDecryptResponseEncryptBodyProcessor {

    protected final Logger log = LoggerFactory.getLogger(getClass());


    public final String requestBody(HttpInputMessage inputMessage, Charset charset) throws IOException {
        InputStream inputStream = inputMessage.getBody();
        String input = IOUtils.toString(inputStream, charset);
        HttpHeaders httpHeaders = inputMessage.getHeaders();

        HttpEncryptRequest request = JSON.toJavaObject(JSON.parseObject(input),HttpEncryptRequest.class);
        return request.getData().toString();
    }

    public final String decryptRequestBody(HttpInputMessage inputMessage, Charset charset) throws IOException {
        InputStream inputStream = inputMessage.getBody();
        String input = IOUtils.toString(inputStream, charset);
        HttpHeaders httpHeaders = inputMessage.getHeaders();

        DecryptRequest request = JSON.toJavaObject(JSON.parseObject(input),DecryptRequest.class);
        return doDecryptRequestBody(request.getData(), httpHeaders, charset);
    }

    public final EncryptResponse encryptResponseBody(String input, HttpHeaders httpHeaders, Charset charset) {
        return doEncryptResponseBody(input, httpHeaders, charset);
    }

    protected String doDecryptRequestBody(String input, HttpHeaders httpHeaders, Charset charset) {
        log.info("请求密文====>"+input);
        //TODO 请求
        try {
            //RSA 解密AES密钥
            byte[] data = Base64.decodeBase64(input);
            byte[] dd =  RSAUtils.decryptByPrivateKey(data,ConstantsPwdKey.PRIVATE_KEY);
            String res = new String(dd);
            log.info("请求明文====>"+res);
            return  res;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("解密失败",e);
        }

        return  "";
    }

    protected EncryptResponse doEncryptResponseBody(String input, HttpHeaders httpHeaders, Charset charset) {
        //TODO 返回
        log.info("返回明文====>"+input);
        EncryptResponse response = new EncryptResponse();
        try {
            byte[] encodedData = RSAUtils.encryptByPublicKey(input.getBytes(), ConstantsPwdKey.PUBLIC_KEY);
            response.setData(Base64.encodeBase64String(encodedData));
            log.info("请求密文====>"+ JSONObject.toJSONString(response));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("加密失败",e);
        }
        return  response;
    }

}
